package com.afkt.project.ui.slice;


import com.afkt.project.model.bean.MyListBean;
import com.afkt.project.ui.adapter.MyListAdapter;
import com.dev.utils.LogUtils;
import com.dev.utils.app.toast.ToastTintUtils;
import com.ohos.afkt.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class TimerSlice extends AbilitySlice {
    private ListContainer listContainer;
    private MyListAdapter mAdapter;
    private Button goBack;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_timer);
        /**
         *设置返回
         * */
        goBack =(Button) findComponentById(ResourceTable.Id_btn_crash_back);
        if(goBack!=null){
            goBack.setClickedListener(component -> {
              onBackPressed();
            });
        }
        initList();
    }

    private void initList() {
        List<MyListBean> list = initData();
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_ringtone_list);
        mAdapter = new MyListAdapter(list, this);
        listContainer.setItemProvider(mAdapter);

        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            private Timer timer1 ;
            private Timer timer2 ;
            private Integer count = 0;
            private boolean flag;
            private final List<Timer> mTimerLists = new ArrayList<>();


            @Override
            public void onItemClicked(ListContainer listContainer, Component component,
                                      int position, long l) {
                switch (position) {
                    //启动定时器
                    case 0:
                        if(!flag){
                            timer1 = new Timer();
                            timer1.schedule(new TimerTask() {
                                @Override
                                public void run() {
                                    count++;
                                    LogUtils.info("myTimer","======每隔5秒触发一次  已经触发次数："+count);
                                }
                            }, 0, 5000);
                            flag = true;
                            mTimerLists.add(timer1);
                            new ToastDialog(getContext()).setText("定时器启动成功,请查看Logcat").show();
                        }else{
                            ToastTintUtils.error("定时器已经启动,请查看Logcat");
                        }
                        break;
                    //停止定时器
                    case 1:
                        if(flag){
                            if(timer1 !=null){
                                timer1.cancel();
                                mTimerLists.remove(timer1);
                            }
                            if(timer2 !=null){
                                timer2.cancel();
                                mTimerLists.remove(timer2);
                            }
                            flag = false;
                            count=0;
                            new ToastDialog(getContext()).setText("定时器关闭成功").show();
                        }else{
                            ToastTintUtils.error("定时器未启动");
                        }

                        break;
                    //重新启动定时器
                    case 2:
                        if(flag && timer1 !=null){
                            timer1.cancel();
                            mTimerLists.remove(timer1);
                        }else{
                            if(flag && timer2 !=null){
                                timer2= new Timer();
                                timer2.schedule(new TimerTask() {
                                    @Override
                                    public void run() {
                                        count++;
                                        LogUtils.info("myTimer","======每隔5秒触发一次  已经触发次数："+count);
                                    }
                                }, 0, 5000);
                                mTimerLists.add(timer2);
                                flag = true;
                                new ToastDialog(getContext()).setText("定时器启动成功,请查看Logcat").show();
                                break;
                            }
                            if(!flag && timer1 ==null){
                                ToastTintUtils.error("请先启动定时器");
                                break;
                            }
                        }
                        timer2= new Timer();
                        timer2.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                count++;
                                LogUtils.info("myTimer","======每隔5秒触发一次  已经触发次数："+count);
                            }
                        }, 0, 5000);
                        mTimerLists.add(timer2);
                        flag = true;
                        new ToastDialog(getContext()).setText("定时器启动成功,请查看Logcat").show();
                        break;
                    //定时器是否启动
                    case 3:
                        if(flag){
                            new ToastDialog(getContext()).setText("定时器已经启动").show();
                        }else {
                            ToastTintUtils.error("定时器未启动");
                        }
                        break;

                    //获取定时器
                    case 4:
                        if(flag && (timer1 != null || timer2 != null)){
                            for (int i = 0; i <mTimerLists.size() ; i++) {
                                Timer timer = mTimerLists.get(i);
                                if( timer.equals(timer1) || timer.equals(timer2)){
                                    new ToastDialog(getContext()).setText("获取定时器成功").show();
                                }
                            }
                        }else {
                            ToastTintUtils.error("暂无定时器");
                        }
                        break;
                    case 5:
                        //获取定时器运行次数
                        if(flag){
                            new ToastDialog(getContext()).setText("定时器运行次数:"+count.toString()).show();
                        }else {
                            ToastTintUtils.error("定时器未启动");
                        }
                        break;
                }
            }
        });
    }

    private List<MyListBean> initData() {
        List<MyListBean> lists = new ArrayList<>();
        lists.add(new MyListBean("启动定时器"));
        lists.add(new MyListBean("停止定时器"));
        lists.add(new MyListBean("重新启动定时器"));
        lists.add(new MyListBean("定时器是否启动"));
        lists.add(new MyListBean("获取定时器"));
        lists.add(new MyListBean("获取运行次数"));
        return lists;
    }
}
